package com.example.li_han.servlet;

import com.example.li_han.service.NewsService;
import com.google.gson.Gson;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

// GetNewsByType.java - 根据分类名称获取新闻列表
@WebServlet(name = "GetNewsByType", value = "/GetNewsByType")

public class GetNewsByType extends HttpServlet {
    private NewsService newsService = new NewsService();

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        response.setContentType("application/json;charset=utf-8");
        Gson gson = new Gson();
        try {
            String typeName = request.getParameter("typeName");
            String pageStr = request.getParameter("page");
            String pageSizeStr = request.getParameter("pageSize");

            // 验证必填参数
            if (typeName == null || typeName.trim().isEmpty()) {
                response.getWriter().write("{\"code\":400,\"message\":\"分类名称不能为空\"}");
                return;
            }

            int currentPage = 1; // 默认第一页
            int pageSize = 10;   // 默认每页10条

            // 解析分页参数
            if (pageStr != null && !pageStr.trim().isEmpty()) {
                currentPage = Integer.parseInt(pageStr);
            }
            if (pageSizeStr != null && !pageSizeStr.trim().isEmpty()) {
                pageSize = Integer.parseInt(pageSizeStr);
            }

            // 验证参数有效性
            if (currentPage <= 0) {
                currentPage = 1;
            }
            if (pageSize <= 0) {
                pageSize = 10;
            }

            // 获取新闻列表 - 使用分类名称查询
            java.util.List<com.example.li_han.pojo.News> newsList = newsService.getNewsByType(typeName);

            // 手动分页处理
            int totalCount = newsList.size();
            int totalPages = (int) Math.ceil((double) totalCount / pageSize);
            int startIndex = (currentPage - 1) * pageSize;
            int endIndex = Math.min(startIndex + pageSize, totalCount);

            // 分页截取
            if (startIndex < totalCount) {
                newsList = newsList.subList(startIndex, endIndex);
            } else {
                newsList = java.util.Collections.emptyList();
            }

            // 构建响应数据
            java.util.HashMap<String, Object> result = new java.util.HashMap<String, Object>();
            result.put("code", 200);
            result.put("message", "获取成功");
            result.put("data", newsList);
            int finalCurrentPage = currentPage;
            int finalPageSize = pageSize;
            result.put("pagination", new java.util.HashMap<String, Object>() {{
                put("currentPage", finalCurrentPage);
                put("pageSize", finalPageSize);
                put("totalCount", totalCount);
                put("totalPages", totalPages);
            }});

            String json = gson.toJson(result);
            response.getWriter().write(json);

        } catch (NumberFormatException e) {
            response.getWriter().write("{\"code\":400,\"message\":\"参数格式错误\"}");
        } catch (Exception e) {
            e.printStackTrace(); // 打印错误日志
            response.getWriter().write("{\"code\":500,\"message\":\"服务器内部错误\"}");
        }
    }
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException {}
}
